******************************
* Table A3- Balance table for sample with attrition
* col 1, 2, 3, 4
******************************
clear all
set trace off
set more off
use Jehanabad_analytic_sample_cognition
drop if wave==1

clear matrix
mat A1 = J(38,1,.)
mat A1a = J(38,1,.)
mat A2 = J(38,1,.)
mat A2a = J(38,1,.)
mat A3 = J(38,1,.)
mat A3a = J(38,1,.)
mat A4 = J(38,1,.)
mat A5 = J(38,1,.)
mat A6 = J(38,1,.)

local n 1

*hemoglobin contious
foreach var in hemoglobin{
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local mean
}

*forms of anemia dummy
foreach var in any_an mild_an mod_sev_an{
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'



local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local num
local num0
local num1
local mean
}

**Cognitive test outcomes

foreach var in block_design digit_span_forward digit_span_backward progressive_matrices day_and_night Cognition_Score_pca math reading total_attendance_2 {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}

** Panel B
* Demographics and socioeconomic variables
foreach var in muslim_HH sc_st block rural_HH no_HHmembers years_schooling_father years_schooling_mother asset_index distance_school sex_child {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}

* Health care / nutrition
foreach var in institut_delivery health_insurance diarrhea improved_sanitation diet_diversity_score no_meals cut_meals health_knowledge {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}

** Caretaking behavior
foreach var in help_homework time_physical_care school_meetings father_at_home {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}

**Panel C
* School characteristics

collapse total_enrollment class_size student_teacher_ratio calories_ph iron_ph treat, by (school_id)
foreach var in total_enrollment class_size student_teacher_ratio {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local mean
}


matrix rownames A4 = "Hemoglobin" "Any anemia" "Mild anemia" "Moderate or severe anemia" "Block design" "Digit span forward" "Digit span backward" "Progressive matrices" "Day and night" "Cognition score index" "Math" "Reading" "School attendance" "Muslim HH" "Sc/st" "Block" "Rural HH" "No of HH members" "Years schooling father" "Years schooling mother" "Asset index" "Distance to school" "Male child" "Institutional delivery" "Health insurance" "Diarrhea" "Improved sanitation" "Diet diversity score" "Number of meals" "Cut meals" "Maternal health knowledge" "Help with homework" "Time physical care" "School meetings" "Father at home"  "Total enrollment" "Class size" "Student teacher ratio"            
frmttable , statmat(A4) replace sdec(0) title (Table A3: Baseline summary statistics  for education sample (balance check)) ctitles("", " N (1)") dwide note("This table presents summary statistics as well as p-values for the difference in the means t-tests between children in the treatment and the control schools. All variables shown are child-level characteristics from the baseline except for panel C, which shows school-level variables. SD: Standard deviation; SC/ST: Scheduled Caste/Scheduled Tribe are the social and economically disadvantaged groups; N: Number of observations. One school was dropped from the control group between the survey period. ")  
frmttable , statmat(A2) replace sdec(2) merge ctitles(" Control mean (2)")
frmttable , statmat(A2a) replace sdec(2) merge ctitles(" Control (SD)")  
frmttable , statmat(A3) replace sdec(2) merge ctitles("Treatment mean (3) ") 
frmttable , statmat(A3a) replace sdec(2) merge ctitles(" Treatment (SD)") 
frmttable , statmat(A5) replace sdec(3) merge ctitles("p-values (T-C) (4) ")  


frmttable using tableA3_balance_cognitionsample.doc, replay replace

*****************************
*** generate col(5-8), Table A3**
*********************************
use Jehanabad_analytic_sample_cognition_attrition.dta
run Table A3.do on this sample to generate col 5-8 of table A3

